package com.softtown.modules.sys.controller;

import com.github.pagehelper.PageInfo;
import com.softtown.common.annotation.LogOperation;
import com.softtown.common.utils.Result;
import com.softtown.common.validator.ValidatorUtils;
import com.softtown.modules.sys.dto.SysMessageDTO;
import com.softtown.modules.sys.dto.SysMessageUnreadDTO;
import com.softtown.modules.sys.service.SysMessageService;
import com.softtown.modules.sys.vo.SysMessageNoReadVO;
import com.softtown.modules.sys.vo.SysMessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息controller
 *
 * @author Dingyayong
 * @version v1.0
 * @create 2019-07-09 17:04
 **/
@RestController
@RequestMapping("/sys/message")
@Api(tags="消息")
public class SysMessageController {

    @Resource
    private SysMessageService sysMessageService;

    @PostMapping("getMessageList")
    @ApiOperation("消息列表")
    @LogOperation("消息列表")
    public Result getGuestMenu(@RequestBody SysMessageDTO sysMessageDTO){
        return new Result<PageInfo<SysMessageVO>>().ok(sysMessageService.getMessageList(sysMessageDTO));
    }

    @GetMapping("{id}")
    @ApiOperation("删除消息")
    @LogOperation("删除消息")
    public Result deleteMessage(@PathVariable("id") Long id){
        return sysMessageService.deleteMessage(id);
    }

    @PostMapping("readMessage")
    @ApiOperation("已读消息")
    @LogOperation("已读消息")
    public Result readMessage(@RequestBody JSONObject jsonObject) {
        String jsonId = jsonObject.get("id").toString();
        Long messageId = Long.parseLong(jsonId);
        return sysMessageService.updateReadMessage(messageId);
    }

    @PostMapping("setUnread")
    @ApiOperation("标记为未读消息")
    @LogOperation("标记为未读消息")
    public Result setUnread(@RequestBody SysMessageUnreadDTO sysMessageUnreadDTO) {
        ValidatorUtils.validateEntity(sysMessageUnreadDTO);
        return sysMessageService.updateNotReadMessage(sysMessageUnreadDTO);
    }

    @PostMapping("notReadMessage")
    @ApiOperation("查询未读消息")
    @LogOperation("查询未读消息")
    public Result notReadMessage(){
        return new Result<>().ok(sysMessageService.queryNoReadMessage());
    }
}

